package com.ocom.srvpaccess.controller;


import com.ocom.common.constant.SrvpAccessDeviceUrl;
import com.ocom.common.entity.access.EntityMjDeviceInfo;
import com.ocom.common.entity.access.EntityMjYkm;
import com.ocom.common.enums.MachEnum;
import com.ocom.common.resp.RespMachMessage;
import com.ocom.common.utils.RespUtilMach;
import com.ocom.srvpaccess.service.MjDeviceInfoService;
import com.ocom.srvpaccess.service.MjYkmService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@RestController
public class YkmController {
    private final Logger log = LoggerFactory.getLogger(YkmController.class);

    @Autowired
    private MjYkmService mjYkmService;

    @Autowired
    private MjDeviceInfoService deviceInfoService;
    /**
     * 机器获取粤康码规则信息（Ykm/machine）
     * */
    @GetMapping(SrvpAccessDeviceUrl.URLP_ACCESSDEVICE_YkmMachine)
    public RespMachMessage YkmMachine(Long comId, String deviceNo){
        if (comId == null){
            log.error("单位编号为空");
            RespUtilMach.error(MachEnum.PARAMS_ERROR);
        }
        if (deviceNo == null){
            log.error("机器编号为空");
            RespUtilMach.error(MachEnum.PARAMS_ERROR);
        }
        // 1.分库查找 mj_device_info 中 device_no 对应的 area_id
        EntityMjDeviceInfo deviceInfo = null;
        try {
            deviceInfo = deviceInfoService.getDeviceInfo(comId, deviceNo);
            if (deviceInfo == null){
                log.error("获取设备信息为空");
                RespUtilMach.error(MachEnum.NOT_MACH);
            }
        }catch (Exception exception){
            log.error("获取设备信息失败："+exception.getMessage());
            RespUtilMach.error(MachEnum.MACH_NOT_INFO);
        }

        EntityMjYkm entityMjYkm = null;
        try {
            //1.查找 info  select * from mj_ykm where company_id = #companyId  and  area_id =# area_id
            entityMjYkm = mjYkmService.YkmInfo(comId, deviceInfo.getAreaId());
            if (entityMjYkm == null){
                //3.如果为空 查找  select * from mj_ykm where company_id = #companyId  and  area_id =0
                entityMjYkm = mjYkmService.YkmInfo(comId, 0L);
                if (entityMjYkm == null){
                    try {
                        mjYkmService.initYkmInfo(comId,0L);
                    }catch (Exception exception){
                        log.error("初始化粤康码规则信息失败"+exception.getMessage());
                        return  RespUtilMach.error(MachEnum.NOT_INIT_YKM);

                    }
                }
                // 2.如果不为空则返回
                return RespUtilMach.success(entityMjYkm);
            }
        }catch (Exception exception){
            log.error("获取粤康码规则信息失败："+exception.getMessage());
            return  RespUtilMach.error(MachEnum.NOT_INFO_YKM);
        }
        return RespUtilMach.success(entityMjYkm);
    }






}
